package test.lyj;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import test.lyj.model.ListAdapter;
import test.lyj.tool.ImgFileService;
import test.lyj.tool.Tool;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;

public class ImgBaseActivity extends Activity {
	ImageView imgView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.img_base_window);
		Button logonButton = (Button) findViewById(R.id.btn_img);
		logonButton.setOnClickListener(imgListener);
		imgView = (ImageView) findViewById(R.id.btn_imgshow);
		/*Bitmap mBitmap = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.balloons);
		imgView.setImageBitmap(mBitmap);*/
		
		Button matrixButton = (Button) findViewById(R.id.btn_testmatrix);
		matrixButton.setOnClickListener(imgMatrixListener);
	}
	
	OnClickListener imgMatrixListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			showColorMatrix();
			// TestLyjLunch.this.finish();

		}
	};
	private int mAngle=0;
	private void showColorMatrix() {
		Bitmap mBitmap = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.loading);
		Log.d(this.getClass().getName(), "balloons_w:"+mBitmap.getWidth()+"balloons_h:"+mBitmap.getHeight());
		Log.d(this.getClass().getName(), "mAngle:"+mAngle);
		Bitmap tempBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(tempBitmap);
		//canvas.drawBitmap(mBitmap, 0, 0, null);
		//canvas.setBitmap(mBitmap);
		Paint paint = new Paint();
		
         float x = 20;
         float y = 20;
         
         //canvas.drawColor(Color.WHITE);
         
         //paint.setColorFilter(null);
        // canvas.drawBitmap(mBitmap, 0, 0, paint);
         
         ColorMatrix cm = new ColorMatrix();
         
         mAngle += 2;
         if (mAngle > 180) {
             mAngle = 0;
         }
         
         //convert our animated angle [-180...180] to a contrast value of [-1..1]
         float contrast = mAngle / 180.f;
         
         setContrast(cm, contrast);
         paint.setColorFilter(new ColorMatrixColorFilter(cm));
         canvas.drawBitmap(mBitmap, 0, 0, paint);
         imgView.setImageBitmap(tempBitmap);
	}
	 private static void setContrast(ColorMatrix cm, float contrast) {
         float scale = contrast + 1.f;
            float translate = (-.5f * scale + .5f) * 255.f;
         cm.set(new float[] {
                scale, 0, 0, 0, translate,
                0, scale, 0, 0, translate,
                0, 0, scale, 0, translate,
                0, 0, 0, 1, 0 });
     }

	OnClickListener imgListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			showDialog(1);
			// TestLyjLunch.this.finish();

		}
	};
	private int choiceValue=0;
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case 1:
			final String[] ms = new String[] { "打开图库", "打开照相机", "插入测试图", "一帘幽梦",
					"世界大观" };
			//choiceValue = AppData.temp_userCard.getCardboxStyle().getCardStyleIndex();
//			int choiceint = Integer.parseInt(choiceValue);
			return new AlertDialog.Builder(ImgBaseActivity.this).setIcon(
					R.drawable.icon).setTitle("选择操作方式").setItems(
					ms, new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							doOperate(whichButton);
						}
					}).setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
								
						} 
					}).create();
		}
		/*	
		case 2:
			CharSequence c="从相册选取";
			CharSequence c2="照相机";
			CharSequence[] ds=new CharSequence[2];
			ds[0]=c;
			ds[1]=c2;
			return new AlertDialog.Builder(ModifyCard.this).setIcon(
					R.drawable.info2).setTitle("选择头像").setItems(ds,  new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
//							Tool.showMsgDialog(ModifyCard.this, String.valueOf(whichButton));
							if(whichButton==0){
//								findViewById(R.id.la_b5).setBackgroundResource(R.drawable.ysj);
//								Intent i2 = new Intent();
//								i2.setClass(ModifyCard.this, ChoiceHeadImg.class);
//								startActivityForResult(i2, 0);
								
								Intent i=new Intent(Intent.ACTION_GET_CONTENT);
								i.setType("image/*");
								Intent choose=Intent.createChooser(i, "test");
								startActivityForResult(choose, 0);
								
							}else if(whichButton==1){
								//Mms.
								Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
								intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
								ImgFileService.Path_LocalCardImg, "temp2.png"))); //存放位置为sdcard卡上文件名为temp.png格式
								startActivityForResult(intent, 2);
							}
						}  
					}).setPositiveButton(R.string.str_quxiao,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,
								int whichButton) {
							
						}
					}).create();
			
		}*/
		return null;
	}
	private void doOperate(int whichButton) {
		switch (whichButton) {
		case 0:
			Intent i=new Intent(Intent.ACTION_GET_CONTENT);
			i.setType("image/*");
			Intent choose=Intent.createChooser(i, "test");
			startActivityForResult(choose, 0);
			break;
		case 1:
			Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
			intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
			"/sdcard/DCIM", "temp2.png"))); //存放位置为sdcard卡上文件名为temp.png格式
			startActivityForResult(intent, 2);
			break;
		case 2:
			Bitmap temp=BitmapFactory.decodeResource(getResources(), R.drawable.mpbg1);
			Tool.addImg2DB(ImgBaseActivity.this,null,temp);
	
		}
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == 0) {
			if (data != null) {
			
					//从图库中返回的数据
					Uri uri=data.getData();
					if(Tool.DEBUG)
						Log.d(this.getClass().getName(), "data != null"+uri.getPath());
					Cursor cursor = getContentResolver().query(uri, new String[]{Images.Media.DATA},    
                             null, null, null); 
					String[] ss=cursor.getColumnNames();
				
						cursor.moveToFirst();    
						for (int i = 0; i < ss.length; i++) {
							if(Tool.DEBUG)
								Log.d(this.getClass().getName(), "ss != "+i+"/"+ss[i]+"/"+cursor.getString(cursor.getColumnIndex(ss[i])));
						}
						String imageFilePath = cursor.getString(cursor.getColumnIndex(Images.Media.DATA));  
						Bitmap bm=ImgFileService.findBitmapByPath2(imageFilePath,60,60,true);
						if(Tool.DEBUG)
							Log.d(this.getClass().getName(), "bm size = "+bm.getHeight()+"/"+bm.getWidth());
//						ImgFileService.saveHeadImg2Local(AppData.opCur_user.getSysId(), bm);
//						if(bm.isRecycled()==false) //如果没有回收    
//							bm.recycle();      
						cursor.close();   
						imgView.setImageBitmap(bm);
//						Bitmap mybm=ImgFileService.findHeadImgByName(AppData.opCur_user.getSysId());
						
//						Bitmap bm=ImgFileService.findBitmapByPath(imageFilePath);
						
//				}
			}

		} else if (requestCode == 1) {
			

		}else if(requestCode==2){//处理拍照的返回内容
			/*//2.0以后用这个
			Bitmap bm = ImgFileService.findBitmapByPath2(ImgFileService.Path_LocalCardImg+"/temp2.png", 60,60,true);
			try {
				//1.5用这个
//				Bitmap bm=(Bitmap) data.getExtras().get("data");
				bm=ImgFileService.cutBitmap(bm, 60, 60);
				if(CommonUtil.DEBUG)
					Log.d(TAG, "bm=="+bm.getHeight());
					
				if(bm!=null){
					bm = ImgFileService.cutBitmap(bm, 60, 60);
					AppData.temp_userCard.setHeadBitmap(bm);
					imgHead.setImageBitmap(bm);
					hasSaved = false;
					saveBtn.setVisibility(View.VISIBLE);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}*/
		}
	}

}
